package net.wanho.systemSettings.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.systemSettings.dao.PositionDAO;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.po.Position;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PositionDAOImpl extends BaseDAOImpl<Position> implements PositionDAO {
    @Override
    public List<Position> selectByCond(Map<String, String> condMap) throws DAOException {
        try {
            //基础SQL
            String sql = "SELECT\n" +
                    "  POSITION_ID       positionId,\n" +
                    "  POSITION_NAME     positionName,\n" +
                    "  POSITION_LEVEL    positionLevel,\n" +
                    "  CREATE_TIME       createTime,\n" +
                    "  UPDATE_TIME       updateTime\n" +
                    "FROM emm_position\n" +
                    "WHERE 1 = 1 ";
            //动态拼接SQL
            StringBuffer sb = new StringBuffer(sql);
            //条件无效  或者 没有条件
            boolean isCond = condMap == null || condMap.isEmpty();
            //创建集合 存放 动态参数值 ？ 替换的值
            List<Object> condParamValueList = new ArrayList<>();
            //有效条件
            if (!isCond) {
                condMap.forEach((k, v) -> {
                    sb.append(" AND ").append(k).append(" LIKE  ?");
                    condParamValueList.add("%" + v + "%");
                });
            }
            Object[] params = condParamValueList.toArray();
            return super.queryForList(sb.toString(), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public Position findById(String id) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  POSITION_ID       positionId,\n" +
                    "  POSITION_NAME     positionName,\n" +
                    "  POSITION_LEVEL    positionLevel,\n" +
                    "  CREATE_TIME       createTime,\n" +
                    "  UPDATE_TIME       updateTime\n" +
                    "FROM emm_position\n" +
                    "WHERE POSITION_ID = ?";
            Object[] params = {id};
            return super.queryForObject(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public long count(String positionName) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  COUNT(*)\n" +
                    "FROM emm_position\n" +
                    "WHERE POSITION_NAME = ?";
            Object[] params = {positionName};
            return super.jdbcTemplate.queryForLong(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public long countPositionIdRef(String positionId) throws DAOException {
        try {
            String sql = "SELECT SUM(cut) FROM\n" +
                    "(\n" +
                    "SELECT COUNT(*) cut FROM employee WHERE POSITON_ID = ?\n" +
                    "UNION ALL\n" +
                    "SELECT COUNT(*) cut FROM position_menu_relations WHERE POSITION_ID = ?\n" +
                    ") t";
            Object[] params = {positionId,positionId};
            return super.jdbcTemplate.queryForLong(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean deletePositionIdRef(String positionId) throws DAOException {
        try {
            String sql = "DELETE FROM position_menu_relations WHERE POSITION_ID =?";
            Object[] params = {positionId};
            return super.jdbcTemplate.delete(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean insertPositionIdRef(Position position) throws DAOException {
        try {
            String sql = "INSERT INTO position_menu_relations (RELATION_ID, POSITION_ID, MENU_ID) VALUES (NULL, ?, ?)";
            List<Menu> menuList = position.getMenuList();
            Object[][] params = new Object[menuList.size()][2] ;
            for (int i = 0; i < menuList.size(); i++) {
                Menu menu = menuList.get(i);
                params[i][0] = position.getPositionId() ;
                params[i][1] = menu.getMenuId() ;
            }
            return super.jdbcTemplate.updateBatch(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean update(Position position) throws DAOException {
        try {
            String sql = "UPDATE emm_position\n" +
                    "SET POSITION_NAME = ?,\n" +
                    "  POSITION_LEVEL = ?,\n" +
                    "  UPDATE_TIME = NOW()\n" +
                    "WHERE POSITION_ID = ?";
            Object[] params = {position.getPositionName(),position.getPositionLevel(),position.getPositionId()};
            return super.jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean insert(Position position) throws DAOException {
        try {
            String sql = "INSERT INTO emm_position\n" +
                    "            (POSITION_ID,\n" +
                    "             POSITION_NAME,\n" +
                    "             POSITION_LEVEL,\n" +
                    "             CREATE_TIME,\n" +
                    "             UPDATE_TIME)\n" +
                    "VALUES (NULL,\n" +
                    "        ?,\n" +
                    "        ?,\n" +
                    "        NOW(),\n" +
                    "        NOW())";
            Object[] params = {position.getPositionName(),position.getPositionLevel()};
            return super.jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean delete(String id) throws DAOException {
        try {
            String sql = "DELETE FROM emm_position WHERE POSITION_ID = ?";
            Object[] params = {id};
            return super.jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
